﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Playables;

public class GameController : MonoBehaviour
{
    public static GameController instance;
    public bool isWin = false;
    public GameObject player;
    public List<GameObject> enemies;
    public List<GameObject> walls;
    public GameObject Boom, BoomDamin, InjuredNormal, InjuredYiji, BoomSmall;
    public PlayableDirector timeline;
    public int lastTimes = 3;
    public int sliderValue = 6;

    private void Awake()
    {
        instance = this;
    }

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        List<GameObject> listTemp = GameObject.FindGameObjectsWithTag("Enemy").ToList();
        enemies.Clear();
        for (int i = 0; i < listTemp.Count; i++)
            if (listTemp[i].GetComponent<Enemy>().enemyType != Enemy.EnemyType.Woman)
                enemies.Add(listTemp[i]);
        walls = GameObject.FindGameObjectsWithTag("Wall").ToList();
    }

    public void ReduceTimes()
    {
        if (!isWin)
        {
            if (sliderValue > 1)
                sliderValue = sliderValue - 1;
            Message ms = new Message(MsgType.ReduceSlider);
            ms.send();
            lastTimes = lastTimes - 1;
            Message m = new Message(MsgType.ReduceFeibiao);
            m.send();
            if (lastTimes > 0)
            {
                if (player != null) player.GetComponent<Player>().Init();
            }
            else
                UIController.instance.OpenUIFailed();
        }
    }

    public void ReduceEnemy(GameObject go)
    {
        for (int i = enemies.Count - 1; i >= 0; i--)
        {
            if (enemies[i] == go)
            {
                enemies.Remove(enemies[i]);
                if (enemies.Count <= 0)
                {
                    isWin = true;
                    UIController.instance.OpenUIReward();
                }
            }
        }
    }

    public void ReduceWall(GameObject go)
    {
        for (int i = walls.Count - 1; i >= 0; i--)
        {
            if (walls[i] = go)
            {
                if (walls[i].GetComponent<WallParent>() != null)
                {
                    if(walls[i].GetComponent<WallParent>().effect != null)
                    {
                        walls[i].GetComponent<WallParent>().effect.SetActive(true);
                        walls[i].GetComponent<WallParent>().effect.GetComponent<ParticleSystem>().Play();
                    }
                }
                Destroy(walls[i]);
                walls.Remove(walls[i]);
            }
        }
    }
}